import { _decorator, Component, instantiate, Node, NodePool, Prefab } from 'cc';
import { Tank } from '../tank/Tank';
import { Bullet } from '../Bullet';
const { ccclass, property } = _decorator;

@ccclass('BulletManager')
export class BulletManager extends Component {
    @property(Prefab)
    public bulletPrefab: Prefab = null

    public nodePool: NodePool = new NodePool()

    createBullet(tank: Tank) {
        let bullet = this.nodePool.get()
        if (!bullet) {
            bullet = instantiate(this.bulletPrefab)
        }
        bullet.parent = this.node
        bullet.getComponent(Bullet).initWithDirection(tank.direction)
        bullet.getComponent(Bullet).camp = tank.getCamp()
        bullet.worldPosition = tank.node.worldPosition

        return bullet
    }

    pushBullet(bullet: Node) {
        if (bullet) {
            this.nodePool.put(bullet)
        }
    }

    onDestroy() {
        this.nodePool.clear()
    }
}


